/*
 * Copyright 2021 ZhongChao Blockchain Technology Research Institute.
 *
 * Copyright ConsenSys AG.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * This file has been modified by ZhongChao Blockchain Technology Research Institute to add support for Brochain.
 *
 * SPDX-License-Identifier: Apache-2.0
 */
package link.luyu.plugin.brochain.trie;

import org.apache.tuweni.bytes.Bytes;

public class PutVisitor<V> implements PathNodeVisitor<V> {
    private final NodeFactory<V> nodeFactory;
    private final V value;

    public PutVisitor(final NodeFactory<V> nodeFactory, final V value) {
        this.nodeFactory = nodeFactory;
        this.value = value;
    }

    @Override
    public Node<V> visit(final ExtensionNode<V> extensionNode, final Bytes path) {
        final Bytes extensionPath = extensionNode.getPath();

        final int commonPathLength = extensionPath.commonPrefixLength(path);
        assert commonPathLength < path.size()
                : "Visiting path doesn't end with a non-matching terminator";

        if (commonPathLength == extensionPath.size()) {
            final Node<V> newChild =
                    extensionNode.getChild().accept(this, path.slice(commonPathLength));
            return extensionNode.replaceChild(newChild);
        }

        // path diverges before the end of the extension - create a new branch

        final byte leafIndex = path.get(commonPathLength);
        final Bytes leafPath = path.slice(commonPathLength + 1);

        final byte extensionIndex = extensionPath.get(commonPathLength);
        final Node<V> updatedExtension =
                extensionNode.replacePath(extensionPath.slice(commonPathLength + 1));
        final Node<V> leaf = nodeFactory.createLeaf(leafPath, value);
        final Node<V> branch =
                nodeFactory.createBranch(leafIndex, leaf, extensionIndex, updatedExtension);

        if (commonPathLength > 0) {
            return nodeFactory.createExtension(extensionPath.slice(0, commonPathLength), branch);
        } else {
            return branch;
        }
    }

    @Override
    public Node<V> visit(final BranchNode<V> branchNode, final Bytes path) {
        assert path.size() > 0 : "Visiting path doesn't end with a non-matching terminator";

        final byte childIndex = path.get(0);
        if (childIndex == CompactEncoding.LEAF_TERMINATOR) {
            return branchNode.replaceValue(value);
        }

        final Node<V> updatedChild = branchNode.child(childIndex).accept(this, path.slice(1));
        return branchNode.replaceChild(childIndex, updatedChild);
    }

    @Override
    public Node<V> visit(final LeafNode<V> leafNode, final Bytes path) {
        final Bytes leafPath = leafNode.getPath();
        final int commonPathLength = leafPath.commonPrefixLength(path);

        // Check if the current leaf node should be replaced
        if (commonPathLength == leafPath.size() && commonPathLength == path.size()) {
            return nodeFactory.createLeaf(leafPath, value);
        }

        assert commonPathLength < leafPath.size() && commonPathLength < path.size()
                : "Should not have consumed non-matching terminator";

        // The current leaf path must be split to accommodate the new value.

        final byte newLeafIndex = path.get(commonPathLength);
        final Bytes newLeafPath = path.slice(commonPathLength + 1);

        final byte updatedLeafIndex = leafPath.get(commonPathLength);

        final Node<V> updatedLeaf = leafNode.replacePath(leafPath.slice(commonPathLength + 1));
        final Node<V> leaf = nodeFactory.createLeaf(newLeafPath, value);
        final Node<V> branch =
                nodeFactory.createBranch(updatedLeafIndex, updatedLeaf, newLeafIndex, leaf);
        if (commonPathLength > 0) {
            return nodeFactory.createExtension(leafPath.slice(0, commonPathLength), branch);
        } else {
            return branch;
        }
    }

    @Override
    public Node<V> visit(final NullNode<V> nullNode, final Bytes path) {
        return nodeFactory.createLeaf(path, value);
    }
}
